<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PersonId_FormItem" prop="Entity.PersonId">
								<template #label>
									<WtmLabelTooltip label="员工" tooltip="关联的员工实体" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.PersonId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysPersons" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SchemeId_FormItem" prop="Entity.SchemeId">
								<template #label>
									<WtmLabelTooltip label="社保方案" tooltip="关联的社保方案" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.SchemeId" disabled class="width100">
									<el-option
										v-for="item in stateData.AllSysPersonSocialSecuritySchemes"
										:key="item.Value"
										:value="item.Value"
										:label="item.Text"
									></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>参保信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SocialSecurityNo_FormItem" prop="Entity.SocialSecurityNo">
								<template #label>
									<WtmLabelTooltip label="社保编号" tooltip="社保个人编号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SocialSecurityNo" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HousingFundAccount_FormItem" prop="Entity.HousingFundAccount">
								<template #label>
									<WtmLabelTooltip label="公积金账号" tooltip="公积金个人账号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.HousingFundAccount" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_EnrollmentDate_FormItem" prop="Entity.EnrollmentDate">
								<template #label>
									<WtmLabelTooltip label="参保日期" tooltip="首次参保日期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.EnrollmentDate" disabled class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Status_FormItem" prop="Entity.Status">
								<template #label>
									<WtmLabelTooltip label="参保状态" tooltip="当前参保状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Status" disabled class="width100">
									<el-option key="正常缴纳" value="正常缴纳" label="正常缴纳"></el-option>
									<el-option key="暂停缴纳" value="暂停缴纳" label="暂停缴纳"></el-option>
									<el-option key="已停保" value="已停保" label="已停保"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>缴费信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_PaymentMonth_FormItem"
								prop="Entity.PaymentMonth"
								:rules="[{ required: true, message: '缴费年月为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="缴费年月" tooltip="社保缴费年月(YYYY-MM)" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.PaymentMonth" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>其他</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
								<template #label>
									<WtmLabelTooltip label="备注" tooltip="备注说明" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Remark" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysPersonSocialSecurityApi as api } from '/@/views/person/syspersonsocialsecurity/api';
import other from '/@/utils/other';
import { ISysPersonSocialSecurityVm } from '/@/views/person/syspersonsocialsecurity/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonSocialSecurityVm>>({
	vmModel: {
		Entity: {
			ID: null,
			PersonId: null,
			SchemeId: null,
			SocialSecurityNo: null,
			HousingFundAccount: null,
			EnrollmentDate: null,
			Status: null,
			PaymentMonth: null,
			Remark: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysPersons: [] as ISysSelectType[],
	AllSysPersonSocialSecuritySchemes: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysPersonSocialSecurity/GetSysPersons', [], false).then((x) => (stateData.AllSysPersons = x)),
		other
			.getSelectList('/api/SysPersonSocialSecurity/GetSysPersonSocialSecuritySchemes', [], false)
			.then((x) => (stateData.AllSysPersonSocialSecuritySchemes = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
